package cn.pangolin.pro.framework.common.util.string;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 字符串工具类
 *
 * @author 芋道源码
 */
public class StrUtils {

    private static final String CHARACTERS = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
    private static final Random RANDOM = new Random();

    public static String maxLength(CharSequence str, int maxLength) {
        // -3 的原因，是该方法会补充 ... 恰好
        return StrUtil.maxLength(str, maxLength - 3);
    }

    /**
     * 给定字符串是否以任何一个字符串开始
     * 给定字符串和数组为空都返回 false
     *
     * @param str      给定字符串
     * @param prefixes 需要检测的开始字符串
     * @since 3.0.6
     */
    public static boolean startWithAny(String str, Collection<String> prefixes) {
        if (StrUtil.isEmpty(str) || ArrayUtil.isEmpty(prefixes)) {
            return false;
        }

        for (CharSequence suffix : prefixes) {
            if (StrUtil.startWith(str, suffix, false)) {
                return true;
            }
        }
        return false;
    }

    public static List<Long> splitToLong(String value, CharSequence separator) {
        long[] longs = StrUtil.splitToLong(value, separator);
        return Arrays.stream(longs).boxed().collect(Collectors.toList());
    }

    public static List<Integer> splitToInteger(String value, CharSequence separator) {
        int[] integers = StrUtil.splitToInt(value, separator);
        return Arrays.stream(integers).boxed().collect(Collectors.toList());
    }

    /**
     * 移除字符串中，包含指定字符串的行
     *
     * @param content 字符串
     * @param sequence 包含的字符串
     * @return 移除后的字符串
     */
    public static String removeLineContains(String content, String sequence) {
        if (StrUtil.isEmpty(content) || StrUtil.isEmpty(sequence)) {
            return content;
        }
        return Arrays.stream(content.split("\n"))
                .filter(line -> !line.contains(sequence))
                .collect(Collectors.joining("\n"));
    }

    /**
     * 判断字符串是否为空
     *
     * @param str 入参
     * @return {@link Boolean}
     * @author Rex
     * @since 2021/6/8 15:43
     */
    public static boolean isEmpty(Object str) {
        if (str == null) {
            return true;
        }
        return "".equals(str);
    }

    /**
     * 获取字符串，null返回""
     *
     * @param obj 入参
     * @return {@link String}
     * @author Rex
     * @since 2023/5/29 09:33
     */
    public static String getStr(Object obj) {
        return getStr(obj, "");
    }

    /**
     * 获取字符串，null返回指定字符串
     *
     * @param obj       入参
     * @param defString 为空时返回用
     * @return {@link String}
     * @author Rex
     * @since 2023/5/29 09:34
     */
    public static String getStr(Object obj, String defString) {
        if (isEmpty(obj)) {
            return defString;
        } else {
            return obj.toString();
        }
    }

    /**
     * 使用StringBuilder连接字符串
     *
     * @param args 参数
     * @return {@link String}
     * @author Rex
     * @since 2023/5/29 09:40
     */
    public static String appendString(String... args) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String str : args) {
            stringBuilder.append(str);
        }
        return stringBuilder.toString();
    }

    /**
     * 生成指定长度的随机字符串
     *
     * @param length 长度
     * @return {@link String}
     * @author Rex
     * @since 2025/6/26 15:20
     */
    public static String generateRandomText(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = RANDOM.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(index));
        }
        return sb.toString();
    }


}
